<template>
	<view class="executeResult">
		<uni-nav-bar
			color="#ffffff"
			background-color="#00aded"
			:statusBar="true"
			title="请假管理"
			fixed="true"
			left-icon="left"
			left-text="返回"
			@clickLeft="back"
			@clickRight="arrow"
			right-text="申请"
			right-icon="plus"
		></uni-nav-bar>
		<view class="executeResult_top">
			<!-- 搜索 -->
			<dil-search :columnData="options.columnDataList" @onSearch="onSearch" v-if="options.columnDataList.length > 0"></dil-search>
		</view>
		<view class="executeResult_main">
			<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
		</view>
		<view class="executeResult_floot">
			<uni-popup ref="popup" type="dialog">
				<view class="popContent">
					<view class="main">
						<dil-Form :formId="704" v-model="formValue" labelPosition="left" ref="form"></dil-Form>
						<view class="buttonClass">
							<button type="warn" @click="cancelExcuse" size="mini">取消</button>
							<button type="primary" class="button" @click="onclickConfirm" size="mini">上报</button>
						</view>
					</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			executeResultList: [],
			executeResultColumnData: [],
			formValue: {
				personnelId: null
			},
			options: {
				requestUrl: '/api/v1/tms/getcomprehensiveresults?apiId=681',
				requestQuery: {},
				cardSlotList: [
					{
						label: '驳回',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'refuse',
						arguments: {
							approveKey: 'alternateFields10',
							messageTitle: '是否驳回'
						},
						showCondition: '(item.alternateFields6 == null || item.alternateFields7 == null ) && item.alternateFields10 == null'
					},
					{
						label: '班组长审批',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'excuteMakeTeam',
						arguments: {
							approveKey: 'alternateFields6',
							messageTitle: '是否审批'
						},
						showCondition: 'item.alternateFields6 == null && item.alternateFields10 == null'
					},
					{
						label: '主管审批',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'excuteMakeManager',
						arguments: {
							approveKey: 'alternateFields7',
							messageTitle: '是否审批'
						},
						showCondition: 'item.alternateFields6 != null && item.alternateFields7 == null'
					},
					{
						label: '复岗申请',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'workAppoint',
						arguments: {
							approveKey: 'alternateFields8',
							messageTitle: '是否申请复岗'
						},
						showCondition: 'item.alternateFields7 != null && item.alternateFields8 == null'
					},
					{
						label: '复岗申请审批',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'workApprove',
						arguments: {
							approveKey: 'alternateFields9',
							messageTitle: '是否审批'
						},
						showCondition: 'item.alternateFields8 != null && item.alternateFields9 == null'
					}
				],
				columnDataList: [],
				totalNum: 0,
				rowDataList: []
			}
		};
	},
	onShow() {
		uni.$emit('onShow', 1);

		this.getExecute();
	},
	mounted() {
		this.getExecuteDebounce = this.Tool.debounce((data) => {
			this.getExecute(data);
		}, 1500);
	},
	methods: {
		onSearch(item) {
			this.getExecuteDebounce(item);
		},
		back() {
			// #ifdef APP-PLUS
			uni.navigateBack();
			// #endif

			// #ifdef H5
			this.$router.back();
			// #endif
		},
		arrow() {
			this.formValue.personnelId = uni.getStorageSync('personnelId');
			this.$refs.popup.open();
		},
		cancelExcuse() {
			this.formValue = {};
			this.$refs.popup.close();
		},
		emitEvent(eventName, cardItem) {
			cardItem[cardItem.approveKey] = uni.getStorageSync('personnelName');
			let that = this;
			if (eventName == 'workAppoint') {
				//如果是复岗申请,判断有没有时间
				if (cardItem.alternateFields4 == null) {
					that.formValue = JSON.parse(JSON.stringify(cardItem));
					this.$refs.popup.open();
					return;
				} else {
				}
			}
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '提示',
				content: cardItem.messageTitle,
				success: (res) => {
					if (res.confirm) {
						that.confirmApprove(cardItem);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		//获取执行实绩
		async getExecute(data = {}) {
			data.resultType = '请假';
			data.personnelName = data.personnelName || uni.getStorageSync('personnelName');
			data.personnelPost = data.personnelPost || uni.getStorageSync('personnelPost');
			data.personnelTeam = data.personnelTeam || uni.getStorageSync('personnelTeam');
			this.options.requestQuery = { ...data };
		},
		onclickConfirm() {
			let that = this;
			if (that.formValue.approveKey == 'workAppoint') {
				that.confirmExecute();
			} else {
				this.$refs['form'].validateForm((data) => {
					if (data.flag) {
						uni.showModal({
							cancelText: '取消',
							confirmText: '确认',
							title: '提示',
							content: '确定是否请假',
							success: (res) => {
								if (res.confirm) {
									that.confirmExecute();
								} else if (res.cancel) {
									uni.showToast({
										title: '用户取消操作！',
										icon: 'none',
										duration: 3000
									});
								}
							}
						});
					}
				});
			}
		},
		async confirmExecute() {
			let data = { ...this.formValue };
			if (this.formValue.alternateFields4) {
				data.alternateFields4 = this.formValue.alternateFields4.join('--');
				let startDate = new Date(this.formValue.alternateFields4[0]);
				let endDate = new Date(this.formValue.alternateFields4[1]);
				data.alternateFields13 = (endDate.getTime() - startDate.getTime()) / (60 * 60 * 1000) + '小时';
			}
			data.alternateFields5 = this.formValue.personnelPhone;
			data.resultType = '请假';
			let that = this;
			const res = await this.$myRequest({
				url: '/api/v1/tms/addcomprehensiveresults',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'success'
				});
				setTimeout(() => {
					this.cancelExcuse();
				}, 2000);
				that.getExecute();
			} else {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'error'
				});
			}
		},
		async confirmApprove(item) {
			const res = await this.$myRequest({
				url: '/api/v1/tms/comprehensiveresultsupadete',
				data: item
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'success'
				});
				setTimeout(() => {
					this.cancelExcuse();
				}, 2000);
				this.getExecute();
			} else {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'error'
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.executeResult {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	&_top {
		width: 100vw;
		flex: auto;
	}
	.popContent {
		width: 100vw;
		height: 100%;
		background-color: #fff;
		.main {
			padding-top: 50rpx;
			margin-bottom: 20rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			.buttonClass {
				width: 100%;
				flex: auto;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
}
</style>
